<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Red extends Model
{
    use HasFactory;
    use SoftDeletes;

    protected $fillable = [
        'member_id',
        'red_type',
        'money',
        'pay',
        'charge',
        'pay_type',
        'type',
        'send_coupon',
        'coupon',
        'red_form',
        'red_form_id',
        'number',
        'numbered',
        'title',
        'media',
        'question',
        'answer',
        'award',
        'time_type',
        'start_time',
        'end_time',
        'area_type',
        'area',
        'link',
        'status'
    ];

    protected $casts = [
        'media' => 'array'
    ];

    /**
     * 红包问题和答案结构
     */
    public function setAnswerAttribute($_value)
    {
        $_answer = "";
        if ($_value['question'] == 1) {
            $_answer = json_encode($_value['choice_question']);
        } else if ($_value['question'] == 2) {
            $_answer = json_encode($_value['answer_question']);
        }
        $this->attributes['answer'] = $_answer;
    }

    /**
     * 红包开始时间
     */
    public function setStartTimeAttribute($_value)
    {
        $_start_time = 0;
        if ($_value['time_type'] == 2) {
            $_start_time = $_value['time']["start"];
        }
        $this->attributes['start_time'] = $_start_time;
    }

    /**
     * 红包结束时间
     */
    public function setEndTimeAttribute($_value)
    {
        $_end_time = 0;
        if ($_value['time_type'] == 2) {
            $_end_time = $_value['time']["end"];
        }
        $this->attributes['end_time'] = $_end_time;
    }

    /**
     * 获取红包发布者头像
     */
    public function getOwnerHeaderAttribute()
    {
        return Member::where('id', $this->member_id)->value('avatar');
    }

    /**
     * 获取红包发布者昵称
     */
    public function getOwnerNicknameAttribute()
    {
        return Member::where('id', $this->member_id)->value('nickname');
    }

    /**
     * 红包封面
     */
    public function getCoverAttribute()
    {
        $_cover = "";
        if ($this->red_type != 'default') {
            $_cover = cool_media(Media::where("id", $this->media[0])
                ->value("thumb"));
        }
        return $_cover;
    }

    /**
     * 视频红包获取视频链接
     */
    public function getVideoAttribute()
    {
        $_video = "";
        if ($this->red_type == 'video') {
            return cool_media(Media::where("id", $this->media[0])
                ->value("source"));
        }
        return $_video;
    }

    /**
     * 获取图片红包图片
     */
    public function getImagesAttribute()
    {
        $_images = array();
        if ($this->red_type == 'image') {
            foreach ($this->media as $_v) {
                $_images[] = cool_media(Media::where("id", $_v)->value("source"));
            }
        }
        return $_images;
    }

    /**
     * 推广链接
     */
    public function getLinkAttribute($_value)
    {
        $_link = array();
        if ($_value) {
            $_link = Links::select(array("name", "link"))
                ->where("id", $_value)
                ->first();
        }
        return $_link;
    }

    /**
     * 选择题
     */
    public function getChoiceQuestionAttribute()
    {
        $_answer = array("", 0, "", "");
        if ($this->question == 1) {
            $_answer = json_decode($this->answer, true);
        }
        return $_answer;
    }

    /**
     * 问答题
     */
    public function getAnswerQuestionAttribute()
    {
        $_answer = array("", "");
        if ($this->question == 2) {
            $_answer = json_decode($this->answer, true);
        }
        return $_answer;
    }

    /**
     * 红包时间
     */
    public function getTimeAttribute()
    {
        $_time = array("start" => 0, "end" => 0);
        if ($this->start_time) $_time["start"] = $this->start_time;
        if ($this->end_time) $_time["end"] = $this->end_time;
        return $_time;
    }

    /**
     * 当前用户是否开启过红包
     */
    public function getUnderAttribute()
    {
        $_under = "not";
        $_red_record_id = RedRecord::where("pid", 0)
            ->where("red_id", $this->id)
            ->where("member_id", auth('member')->id())
            ->value("id");
        if ($_red_record_id) $_under = "success";
        return $_under;
    }

    /**
     * 红包title，过滤敏感词
     */
    public function getTitleAttribute($_value)
    {
        return sensitiveWord($_value);
    }

    /**
     * 红包源标题
     * @return mixed
     */
    public function getOriginalTitleAttribute()
    {
        return $this->attributes['title'];
    }

    /**
     * 当前用户是否已经提交红包表单
     */
    public function getSubmitFormAttribute()
    {
        if(RedForm::where("red_id",$this->id)
            ->where("member_id",auth('member')->id())
            ->value("id")) {
            return 1;
        }
        return 0;
    }

    /**
     * 当前登录用户是否为发布者
     */
    public function getOwnerAttribute()
    {
        return $this->member_id == auth('member')->id() ? 1 : 0;
    }
}
